package com.example.pt.service.impl;

import com.example.pt.entity.Notification;
import com.example.pt.repository.NotificationRepository;
import com.example.pt.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NotificationServiceImpl implements NotificationService {

    @Autowired
    private NotificationRepository notificationRepository;

    // 根据 ID 获取通知
    public List<Notification> getNotificationById(Long user_id) {
        return notificationRepository.findByUserId(user_id);
    }


    // 创建通知
    public void createNotification(Notification notification) {
        Long user_id=notification.getUser_id();
        String title=notification.getTitle();
        String content=notification.getContent();
        LocalDateTime create_time=notification.getCreate_time();
        notificationRepository.insertNotification(user_id,title, content, create_time);
    }


    // 删除通知
    public void deleteNotification(Long id) {
        notificationRepository.deleteById(id);
    }

    // Service层使用示例
    @Override
    public String updateNotificationStatus(Long id, int status) {
        if (status < 0 || status > 1) {
            return "状态值无效（只允许0-1）";
        }

        int affectedRows = notificationRepository.updateStatus(id, status);

        if (affectedRows > 0) {
            return switch (status) {
                case 0 -> "通知已标记为未读";
                case 1 -> "通知已标记为已读";
                //case 2 -> "通知已删除";
                default -> "状态已更新";
            };
        } else {
            return "状态未变更（可能已是目标状态）";
        }
    }

}
